package com.srit.system.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.srit.core.bean.Page;
import com.srit.system.bean.EffectTime;
import com.srit.system.bean.EffectTimeGroup;
import com.srit.system.dao.EffectTimeDao;
import com.srit.system.dao.EffectTimeGroupDao;

@Controller
public class EffectTimeGroupController {

	@Autowired
	private EffectTimeGroupDao effectTimeGroupDao;

	@Autowired
	private EffectTimeDao effectTimeDao;

	@RequestMapping(value = "/effectTimeGroup-search")
	public String search(@ModelAttribute Page page) {
		effectTimeGroupDao.page(page);
		return "system/effectTimeGroup";
	}

	@RequestMapping(value = "/effectTimeGroup-form")
	public String form(@ModelAttribute EffectTimeGroup effectTimeGroup) {
		return "system/effectTimeGroup";
	}

	@RequestMapping(value = "/effectTimeGroup-edit")
	public String edit(@ModelAttribute EffectTimeGroup effectTimeGroup,
			@RequestParam int id) {
		List<EffectTime> effectTimeList = effectTimeDao.listByGroupId(id);
		EffectTimeGroup tmp = effectTimeGroupDao.getById(id);
		tmp.setEffectTimeList(effectTimeList);
		BeanUtils.copyProperties(tmp, effectTimeGroup);
		return "system/effectTimeGroup";
	}

	@RequestMapping(value = "/effectTimeGroup-saveOrUpdate")
	public String saveOrUpdate(EffectTimeGroup effectTimeGroup) {
		if (effectTimeGroup.getId() != null && effectTimeGroup.getId() != 0) {
			effectTimeGroupDao.update(effectTimeGroup);
		} else {
			effectTimeGroupDao.save(effectTimeGroup);
		}
		return "redirect:/effectTimeGroup-edit?id=" + effectTimeGroup.getId();
	}

}
